<?php
session_start( );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Dynamic Drive DHTML Scripts- Smooth Navigational Menu</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="A folding DHTML menu tree with persistance feature, created via cookies." />
<meta name="keywords" content="DHTML, folding menu tree script, DHTML tutorial, free, JavaScript,persistance" />
<link rel="stylesheet" type="text/css" href="../ddincludes/mainstyle.css" />
<link rel="stylesheet" type="text/css" href="ddsmoothmenu.css" />
<!--[if lte IE 7]>
<style type="text/css">
html .ddsmoothmenu{height: 1%;} /*Holly Hack for IE7 and below*/
</style>
<![endif]-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="ddsmoothmenu.js">
</script>
</head>
<body>
<?php
$db = new mysqli('localhost', 'root', '', 'frcc_test'); 

if (mysqli_connect_errno()) {    die ('Could not open a mysql connection: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
}
$level = $_SESSION['level'];
$user = $_SESSION['myusername'];
echo 'Logon as: ' . $user . '  Security level :' . $level;
$db->query("SET NAMES 'utf8'");
$sql = 'SELECT id, parentId, name, level, linkpage FROM menu where level <= ' ;
$sql .= $level;
$sql .= ' ORDER BY parentId, name ';

$result = $db->query($sql);
if (!$result) 
{    die ('Something is wrong with the query: '.$db->error);
}
//echo 'The result set got '.$result->num_rows." rows<br />\n";

// prepare special array with parent-child relations 
$menuData = array( 
    'items' => array(), 
    'parents' => array(),
); 

while ($menuItem = $result->fetch_assoc()) 
{ //var_export($menuItem);
    $menuData['items'][$menuItem['id']] = $menuItem; 
    $menuData['parents'][$menuItem['parentId']][] = $menuItem['id']; 
}
//echo $PHPSESSID;
//session_register("menuData"); 
$_SESSION['menuData'] = $menuData;
 if (!isset($_SESSION['menuData'])){
 //echo 'session variable is not found';
 $m1 = $_SESSION['menuData']; 
 }else { //echo 'session variable is found<br>';
}
//var_export($m1);
// menu builder function, parentId 0 is the root 
function buildMenu($parentId, $menuData) 
{ 
    $html = ''; 

    if (isset($menuData['parents'][$parentId])) 
    { 
        $html .= '<ul>'; 
        foreach ($menuData['parents'][$parentId] as $itemId) 
        { 
            $html .= '<li><a href="' . $menuData['items'][$itemId]['linkpage']  . '">' . $menuData['items'][$itemId]['name'] . '</a>'; 

            // find childitems recursively 
            $html .= buildMenu($itemId, $menuData); 

            $html .= '</li>'; 
        } 
        $html .= '</ul>'; 
    } 

    return $html; 
} 
// output the menu 
echo '<div id="smoothmenu1" class="ddsmoothmenu">' . buildMenu(0, $menuData) . '<br style="clear: left" /></div>';
unset($result); 
$db->close();
// you can delete the variable with unset() as it isn't needed anymore.
?>
<br>
<a href="showmember.php">Show Member List</a><br>
<a href="newmember.php">Add Member</a><br>
<a href="deletemember.php">Delete Member</a><br>
</body>
</html>
